﻿using System;
using System.IO;

namespace Ehai.Web.Framework.Rules
{
    class DelfileRuleHandle : IMsgRuleHandle
    {
        #region IMsgRuleHandle 成员

        public const string Name = "delfile";

        private int _execStatus = 0;
        public int ExecStatus
        {
            get { return _execStatus; }
        }

        /// <summary>
        /// 删除指定路径下的文件
        /// </summary>
        /// <param name="p">
        /// 传入数组<br />
        /// 0:(必选)处理类型,delfile<br />
        /// 1:(必选)要删除的文件名<br />
        /// 2:(可选)要删除文件的相对路径<br />
        /// 3:(可选)要删除文件的根路径<br />
        /// </param>
        /// <param name="ci">环境变量</param>
        /// <example>
        /// string[] p = { "delfile", "555.txt", "D:\\ehai2009实践\\7.28", "try" };
        /// </example>
        /// <example>
        /// string[] p = { "delfile", "555.txt", "D:\\ehai2009实践\\7.28", ""};
        /// </example>
        /// <example>
        /// string[] p = { "delfile", "555.txt", "", "D:\\ehai2009实践\\7.28"};
        /// </example>
        /// <example>
        /// string[] p = { "delfile", "555.txt", "", ""};//只有两个参数时，若输入多于2个参数("",""),则返回错误
        /// </example>
        /// <example>
        /// string[] p = { "delfile", "555.txt"};
        /// </example>
        /// <example>
        /// string[] p = { "delfile", "666.txt", "D:\\ehai2009实践\\7.28", "try"}; 
        /// </example>
        /// <exception cref="Ehai.Web.Framework.WebException">
        /// Ehai.Web.Framework.WebException:文件删除错误！
        /// </exception>
        /// <returns>返回删除信息</returns>
        public string[][] Execute(string[] p, ContextInfo ci)
        {
            if (p.Length < 2 || p[1].Trim() == String.Empty)
            {
#if DEBUG
                // 操作失败,状态值为1
                _execStatus = 1;
#endif
                return null;
            }

             if (p.Length > 2 && p[2].Trim() == String.Empty)
            {
#if DEBUG
                // 操作失败,状态值为2
                _execStatus = 2;
#endif
                return null;
            }

            if (p.Length > 3 && p[3].Trim() == String.Empty)   //防止被攻击！！
            {
#if DEBUG
                // 操作失败,状态值为3
                _execStatus = 3;
#endif
                return null;
            }

            string p2 = p.Length > 2 ? p[2].Trim() : String.Empty;
            string p3 = p.Length > 3 ? p[3].Trim() : String.Empty;

            try
            {
                string file = Path.Combine(Path.Combine(p3, p2), p[1]);

                if (!File.Exists(file))
                {
#if DEBUG
                    // 操作失败并捕获异常,状态值为-1
                    _execStatus = -1;
#endif
                    throw new Exception("请输入正确的文件路径！！");
                }

                File.Delete(file);
#if DEBUG
                // 操作成功,状态值为0
                _execStatus = 0;
#endif
                return new string[1][] { new string[] { "success" } };
            }
            catch (Exception ex)
            {
#if DEBUG
                // 操作失败并捕获异常,状态值为-1
                _execStatus = -1;
#endif
                throw new Ehai.Web.Framework.WebException("Ehai.WebFramework.DelfileRule", "文件删除错误！", ex);
            }
        }

        public string GetHelpInfo()
        {
            return "删除文件";
        }

        public string GetHelpDetail()
        {
            return "删除指定路径下的文件:<br />" +
                    "例1: { delfile, 555.txt, D:\\ehai2009实践\\7.28, try }; //输入完整路径,返回正确结果<br /> " +
                    "例2: { delfile, 555.txt, D:\\ehai2009实践\\7.28 }; //输入相对路径,返回正确结果<br />" +
                    "例3: { delfile, 555.txt };//输入相对路径,返回正确结果 <br />" +
                    "例4: { delfile, 666.txt, D:\\ehai2009实践\\7.28, try }; //文件不存在，抛出异常<br />" +
                    "返回:删除信息";
        }

        public string[] GetParametersInfo()
        {
            string[] t = new string[4];
            t[0] = "(必选)处理类型,即\"delfile\"";
            t[1] = "(必选)要删除的文件名";
            t[2] = "(可选)要删除文件的相对路径";
            t[3] = "(可选)要删除文件的根路径";
            return t;
        }

        #endregion
    }
}
